// @flow
import React from 'react'
import moment from 'moment'
import styles from './Timeline.css'
import { connect } from 'react-redux'
type Props = {
  timeData: Array<Object>,
  history: Object,
  deviceId: number
}
export class Timeline extends React.Component {
  props: Props
  goToDeviceOverView: Function
  constructor (props: Props) {
    super(props)
    this.goToDeviceOverView = this.goToDeviceOverView.bind(this)
  }
  goToDeviceOverView (data: Object) {
    let channel = []
    if (data.checkName) {
      channel = [data.channelId + '__' + data.checkName.split('__')[1]]
    }
    const component = this
    return () => {
      const { deviceId, history } = component.props
      const location = {
        pathname:`/device-file/${deviceId}/condition`,
        state: {
          defaultActiveKey: 'historyTab',
          // dynamicIndicator: '', // 以前不传指标的时候
          dynamicIndicator: data.sensorNameEn + '___' + data.variableNameEn,
          defaultChannel: channel,
          startTime: data.startTime,
          endTime: data.endTime
        }
      }
      history.push(location)
    }
  }
  render () {
    const { timeData } = this.props
    return (
      <div className={styles['Timeline']}>
        <svg height='5' width='100'>
          <line x1='0' y1='0' x2='100' y2='0' style={{ stroke:'#004165', strokeWidth: 5 }} />
          Sorry, your browser does not support inline SVG.
        </svg>
        <div className={styles['now']} />
        {timeData.map((event: Object, index, list) => {
          const type = index % 2 === 0 ? 1 : 2
          return [
            <div className={styles['event' + type]} key={index + '-1'}
              onClick={this.goToDeviceOverView(event)}>
              <div className={styles['event' + type + 'Bubble']}>
                <div
                  title={event.eventName} className={styles['eventTitle']}>
                  名称：{event.eventName}
                </div>
                <div className={styles['eventContent']}>
                  负责人：{event.owner}<br />
                  处理状态：{event.status}
                </div>
              </div>
              <svg height='20' width='20'>
                <circle cx='10' cy='11' r='5' fill='#004165' />
              </svg>
              <div className={styles['time' + type]}>
                {moment(event.startTime).format('YY-MM-DD HH:mm:ss')}
              </div>
            </div>,
            <svg height='5' width='300' key={index + '-2'}
              style={{ minWidth: '180px' }}
            >
              <line x1='0' y1='0' x2='300' y2='0' style={{ stroke:'#004165', strokeWidth: 5 }} />
              Sorry, your browser does not support inline SVG.
            </svg>
          ]
        })}
        <svg height='5' width='50'>
          <line x1='0' y1='0' x2='50' y2='0' style={{ stroke:'#004165', strokeWidth: 5 }} />
          Sorry, your browser does not support inline SVG.
        </svg>
        <svg height='5' width='50'>
          <line x1='0' y1='0' x2='50' y2='0' style={{ stroke:'#004165', strokeWidth: 5 }} />
        </svg>
        <svg height='20' width='42'>
          <line x1='1' y1='0' x2='1' y2='20' style={{ stroke:'#004165', strokeWidth: 2 }} />
          <circle cx='11' cy='10' r='3' fill='#004165' />
          <circle cx='21' cy='10' r='3' fill='#004165' />
          <circle cx='31' cy='10' r='3' fill='#004165' />
          <line x1='41' y1='0' x2='41' y2='20' style={{ stroke:'#004165', strokeWidth: 2 }} />
        </svg>
      </div>
    )
  }
}

const mapStatesToProps = (state) => {
  return {
    deviceId: state.deviceCard.deviceData.id
  }
}

export default connect(mapStatesToProps, {})(Timeline)
